<?php

class settings extends base {

    private $content;

    public function submenu() {
        $this->submenu = array();
        $this->submenu[0]["name"] = "Directories";
        $this->submenu[0]["callback"] = "directories";
        $this->submenu[0]["icon"] = "icons/cross.png";
        $this->submenu[0]["rights"] = 0;
        $this->submenu[0]["enabled"] = true;
        $this->submenu[1]["name"] = "DB";
        $this->submenu[1]["callback"] = "db";
        $this->submenu[1]["icon"] = "icons/cross.png";
        $this->submenu[1]["rights"] = 0;
        $this->submenu[1]["enabled"] = true;
        $this->submenu[2]["name"] = "Options";
        $this->submenu[2]["callback"] = "options";
        $this->submenu[2]["icon"] = "icons/cross.png";
        $this->submenu[2]["rights"] = 0;
        $this->submenu[2]["enabled"] = true;
        $this->submenu[3]["name"] = "Processing";
        $this->submenu[3]["callback"] = "processing";
        $this->submenu[3]["icon"] = "icons/cross.png";
        $this->submenu[3]["rights"] = 0;
        $this->submenu[3]["enabled"] = true;
        $this->submenu[4]["name"] = "Quality";
        $this->submenu[4]["callback"] = "quality";
        $this->submenu[4]["icon"] = "icons/cross.png";
        $this->submenu[4]["rights"] = 0;
        $this->submenu[4]["enabled"] = true;
        $this->submenu[5]["name"] = "IMDB";
        $this->submenu[5]["callback"] = "imdb";
        $this->submenu[5]["icon"] = "icons/cross.png";
        $this->submenu[5]["rights"] = 0;
        $this->submenu[5]["enabled"] = true;
        $this->submenu[6]["name"] = "Series";
        $this->submenu[6]["callback"] = "series";
        $this->submenu[6]["icon"] = "icons/cross.png";
        $this->submenu[6]["rights"] = 0;
        $this->submenu[6]["enabled"] = true;
        $this->submenu[7]["name"] = "Music";
        $this->submenu[7]["callback"] = "music";
        $this->submenu[7]["icon"] = "icons/cross.png";
        $this->submenu[7]["rights"] = 0;
        $this->submenu[7]["enabled"] = true;
        $this->submenu[8]["name"] = "Torrent";
        $this->submenu[8]["callback"] = "torrent";
        $this->submenu[8]["icon"] = "icons/cross.png";
        $this->submenu[8]["rights"] = 0;
        $this->submenu[8]["enabled"] = true;
        $this->submenu[9]["name"] = "Subtitle";
        $this->submenu[9]["callback"] = "subtitle";
        $this->submenu[9]["icon"] = "icons/cross.png";
        $this->submenu[9]["rights"] = 0;
        $this->submenu[9]["enabled"] = true;
        $this->_submenu($this->submenu);
    }

    public function processing() {
        global $config;
        $this->get_request();
        if (isset($this->requests["Change"]))
            $config = $this->update_config($config);

        $this->page_manager->text_title("Processing");
        $this->page_manager->start_form("form1");
        $default = $config["Processing"]["delete_sample"];
        $this->page_manager->form_add_field("Delete sample", "Delete sample file if found 0=off, 1=on", "delete_sample", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = implode(",", $config["Processing"]["allowed_formats"]);
        $this->page_manager->form_add_field("Allowed formats", "Which formats should be allowed to use, others will be deleted", "allowed_formats", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = implode(",", $config["Processing"]["unwanted_content"]);
        $this->page_manager->form_add_field("Unwanted content", "Unwanted contents are deleted/skipped", "unwanted_content", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = implode(",", $config["Processing"]["unwanted_tags"]);
        $this->page_manager->form_add_field("Unwanted tags", "You don't want filename with this tags", "unwanted_tags", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Processing"]["rename_files"];
        $this->page_manager->form_add_field("Rename files", "Rename files to be called as directories, often not needed 0=off, 1=on", "rename_files", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Processing"]["starting_year"];
        $this->page_manager->form_add_field("Movie year (start)", "Process only movies that is made from start year to end year (including)", "starting_year", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Processing"]["ending_year"];
        $this->page_manager->form_add_field("Movie year (end)", "Process only movies that is made from start year to end year (including)", "ending_year", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $this->page_manager->form_add_button("Change");
        $this->page_manager->end_form();
        return $this->page_manager->web_content;
    }

    public function quality() {
        global $config;
        $this->get_request();
        if (isset($this->requests["Change"]))
            $config = $this->update_config($config);

        $this->page_manager->text_title("Quality");
        $this->page_manager->start_form("form1");
        $default = $config["Quality"]["minimum_quality"];
        $this->page_manager->form_add_field("Minimum quality", "Minimum quality for content, valid: 'cam', 'camrip', 'screener', 'telesync', 'ts', 'dvdscr', 'r5', 'dvdrip', 'dvdr', 'hdtv', 'vodr', 'brrip', 'bdrip', '720p', '1080p'", "minimum_quality", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Quality"]["maximum_quality"];
        $this->page_manager->form_add_field("Maximum quality", "Maximum quality for content, valid: 'cam', 'camrip', 'screener', 'telesync', 'ts', 'dvdscr', 'r5', 'dvdrip', 'dvdr', 'hdtv', 'vodr', 'brrip', 'bdrip', '720p', '1080p'", "maximum_quality", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Quality"]["minimum_file_size"];
        $this->page_manager->form_add_field("Minimum file size", "Minimum file size, valid examples: 1G, 700M, 300000K", "minimum_file_size", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Quality"]["maximum_file_size"];
        $this->page_manager->form_add_field("Maximum file size", "Maximum file size, valid examples: 1G, 700M, 300000K", "maximum_file_size", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $this->page_manager->form_add_button("Change");
        $this->page_manager->end_form();
        return $this->page_manager->web_content;
    }

    public function imdb() {
        global $config;
        $this->get_request();
        if (isset($this->requests["Change"]))
            $config = $this->update_config($config);

        $this->page_manager->text_title("IMDB");
        $this->page_manager->start_form("form1");
        $default = $config["IMDB"]["download_metadata"];
        $this->page_manager->form_add_field("Download metadata", "Download metadata (info file)", "download_metadata", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["IMDB"]["use_imdb_rename"];
        $this->page_manager->form_add_field("Rename using IMDB", "Rename content to match IMDB suggestion, this is not always right, do not recommend to use", "use_imdb_rename", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["IMDB"]["download_pictures"];
        $this->page_manager->form_add_field("Download pictures", "Download pictures", "download_pictures", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["IMDB"]["minimum_rating"];
        $this->page_manager->form_add_field("Minimum rating", "Minimum rating for content", "minimum_rating", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["IMDB"]["minimum_rating_count"];
        $this->page_manager->form_add_field("Minimum votes", "Mainimum amount of voters needed to make minimum rating valid (you don't want rating for 2 voters)", "minimum_rating_count", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $this->page_manager->form_add_button("Change");
        $this->page_manager->end_form();
        return $this->page_manager->web_content;
    }

    public function series() {
        global $config;
        $this->get_request();
        if (isset($this->requests["Change"]))
            $config = $this->update_config($config);

        $this->page_manager->text_title("Series");
        $this->page_manager->start_form("form1");
        $default = $config["Series"]["recognize_series"];
        $this->page_manager->form_add_field("Process series", "If you disable this series won't be detected and will be deleted", "recognize_series", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = implode(",", $config["Series"]["follow"]);
        //$default = $config["Series"]["follow"];
        $this->page_manager->form_add_field("Follow this series", "Explicitly said to follow only this series, others will be discarded", "follow", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Series"]["override_series_destination"];
        $this->page_manager->form_add_field("Override destination", "Override default destination for series", "override_series_destination", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Series"]["minimum_file_size"];
        $this->page_manager->form_add_field("Minimum file size", "Minimum file size, valid examples: 1G, 700M, 300000K", "minimum_file_size", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Series"]["maximum_file_size"];
        $this->page_manager->form_add_field("Maximum file size", "Maximum file size, valid examples: 1G, 700M, 300000K", "maximum_file_size", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $this->page_manager->form_add_button("Change");
        $this->page_manager->end_form();
        return $this->page_manager->web_content;
    }

    public function music() {
        global $config;
        $this->get_request();
        if (isset($this->requests["Change"]))
            $config = $this->update_config($config);

        $this->page_manager->text_title("Music [non-functional yet]");
        $this->page_manager->start_form("form1");
        $default = $config["Music"]["recognize_music"];
        $this->page_manager->form_add_field("Process music", "If you disable this music  won't be detected and will be deleted", "recognize_music", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Music"]["override_music_destination"];
        $this->page_manager->form_add_field("Override destination", "Override destination for music", "override_music_destination", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $this->page_manager->form_add_button("Change");
        $this->page_manager->end_form();
        return $this->page_manager->web_content;
    }

    public function torrent() {
        global $config;
        $this->get_request();
        if (isset($this->requests["Change"]))
            $config = $this->update_config($config);

        $this->page_manager->text_title("Torrent");
        $this->page_manager->start_form("form1");
        $default = $config["Torrent"]["use_torrents"];
        $this->page_manager->form_add_field("Use torrent", "If you disable this torrent won't be downloaded", "use_torrents", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Torrent"]["use_kickass_torrent"];
        $this->page_manager->form_add_field("Use kickass torrent (kat)", "Get feed from kat and put it to automatic download directory for torrent client to pick up", "use_kickass_torrent", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Torrent"]["kickass_feed"];
        $this->page_manager->form_add_field("Kickass feed", "Feed url for kat", "kickass_feed", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Torrent"]["use_piratebay_torrent"];
        $this->page_manager->form_add_field("Use piratebay", "Get feed from piratebay and put it to automatic download directory for torrent client to pick up", "use_piratebay_torrent", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Torrent"]["piratebay_feed"];
        $this->page_manager->form_add_field("Piratebay feed", "Feed url for piratebay", "piratebay_feed", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Torrent"]["check_imdb_rules"];
        $this->page_manager->form_add_field("Comply to IMDB rules", "If torrent does not meet IMDB rules in IMDB section it will not be downloaded", "check_imdb_rules", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $this->page_manager->form_add_button("Change");
        $this->page_manager->end_form();
        return $this->page_manager->web_content;
    }

    public function subtitle() {
        global $config;
        $this->get_request();
        if (isset($this->requests["Change"])) {
            $config = $this->update_config($config);
        }

        $this->page_manager->text_title("Subtitle");
        $this->page_manager->start_form("form1");
        $default = $config["Subtitle"]["download_subtitles"];
        $this->page_manager->form_add_field("Download subtitles", "If 1 it will download subtitles, 0 disables this", "download_subtitles", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = implode(",", $config["Subtitle"]["language"]);
        $this->page_manager->form_add_field("Subtitle language", "Language(s) for subtitle, if you want multiple just separate with commas (etc.. eng,rus), see 3-letter possible values <a href='http://www.loc.gov/standards/iso639-2/php/code_list.php'>here</a>", "language", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $this->page_manager->form_add_button("Change");
        $this->page_manager->end_form();
        return $this->page_manager->web_content;
    }

    public function directories() {
        global $config;
        $this->get_request();
        if (isset($this->requests["Change"]))
            $config = $this->update_config($config);

        $this->page_manager->text_title("Directories");
        $this->page_manager->start_form("form1");
        $default = $config["Directories"]["source"];
        $this->page_manager->form_add_field("Source", "Source for processing (download folder)", "source", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Directories"]["destination"];
        $this->page_manager->form_add_field("Destination", "Destination for movie content", "destination", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Directories"]["destination_tv"];
        $this->page_manager->form_add_field("Destination for TV", "Destination for TV content", "destination_tv", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Directories"]["automatic_torrents"];
        $this->page_manager->form_add_field("Automatic torrents load directory", "Where to save torrent files that is 'watched' by torrent client", "automatic_torrents", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $this->page_manager->form_add_button("Change");
        $this->page_manager->end_form();
        return $this->page_manager->web_content;
    }

    public function db() {
        global $config;
        $this->get_request();
        if (isset($this->requests["Change"]))
            $config = $this->update_config($config);

        $this->page_manager->text_title("DB");
        $this->page_manager->start_form("form1");
        $default = $config["DB"]["use_db"];
        $this->page_manager->form_add_field("Use DB", "Use DB instead of files (recommended) 0=off, 1=on", "use_db", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["DB"]["db_file"];
        $this->page_manager->form_add_field("DB File", "DB file to use (do not change unless you are very sure you need this)", "db_file", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $this->page_manager->form_add_button("Change");
        $this->page_manager->end_form();
        return $this->page_manager->web_content;
    }

    public function options() {
        global $config;
        $this->get_request();
        if (isset($this->requests["Change"]))
            $config = $this->update_config($config);

        $this->page_manager->text_title("Options");
        $this->page_manager->start_form("form1");
        $default = $config["Options"]["run_seconds"];
        $this->page_manager->form_add_field("Run every X seconds", "Run sorter every X seconds, 0 disables internal timer", "run_seconds", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Options"]["copy_delete_files"];
        $this->page_manager->form_add_field("Copy and delete files", "Use copy and delete to move files 0=off, 1=on", "copy_delete_files", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Options"]["move_files"];
        $this->page_manager->form_add_field("Move files", "Use rename to move files 0=off, 1=on", "move_files", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Options"]["log_disk"];
        $this->page_manager->form_add_field("Log to disk", "Write log to file on disk 0=off, 1=on", "log_disk", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $default = $config["Options"]["log_file"];
        $this->page_manager->form_add_field("Log to file", "Write log to this filename", "log_file", $default, "text");
        $this->page_manager->form_add_element("<br /><br />\n");
        $this->page_manager->form_add_button("Change");
        $this->page_manager->end_form();
        return $this->page_manager->web_content;
    }

    private function update_config($config) {
        $requests = $this->requests;
        $page = $requests["page"];
        $submenu = $requests["submenu"];
        unset($requests["page"]);
        unset($requests["submenu"]);
        unset($requests["Change"]);
        foreach ($requests as $request => $value) {
            $config[$submenu][$request] = $value;
            $this->db->delete_rows("settings", "section='{$submenu}' AND name='{$request}'");
            $rows[0] = array("section" => $submenu, "name" => $request, "value" => $value);
            $r = $this->db->insert_rows("settings", $rows);
        }
        return $config;
    }

    public function run() {
        $this->submenu();
        $callback = $this->_run();
        if ($callback)
            $this->content = $this->$callback();
        $this->finish($this->content);
    }

}

?>